package cn.boyu.controller;


import cn.boyu.entity.AssetsAmmeter;
import cn.boyu.entity.Customer;
import cn.boyu.exception.LogicException;
import cn.boyu.query.CustomerQuery;
import cn.boyu.result.AjaxResult;
import cn.boyu.service.IAssetsAmmeterService;
import cn.boyu.service.ICustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yn
 * @since 2022-11-22
 */
@RestController
@CrossOrigin
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IAssetsAmmeterService ammeterService;

    //显示列表
    @GetMapping("/query")
    public AjaxResult query(CustomerQuery qo, HttpServletRequest request) {
        IPage<Customer> page = customerService.queryPage(qo);
        return AjaxResult.success(page);
    }

    //新增
    @PostMapping("save")
    public AjaxResult save(Customer customer) {
        customerService.savecust(customer);
        return AjaxResult.success();
    }

    //修改
    @PostMapping("/update")
    public AjaxResult update(Customer customer) {
        customerService.update(customer);
        return AjaxResult.success();
    }

    //删除
    @PostMapping("/delete")
    public AjaxResult delete(Long customerId) {
        customerService.delete(customerId);
        return AjaxResult.success("删除成功");
    }

    //客户挂接电表(只考虑一对一挂接）
    @PostMapping("/mount")
    public AjaxResult mount(Long customerId,Long ammeterId){
        System.out.println("customerId: " + customerId + "ammeterId: "+ammeterId);
        //判断客户和电表是否已经被挂接
        AssetsAmmeter ammeter = ammeterService.getOne(new QueryWrapper<AssetsAmmeter>().eq("id", ammeterId).isNull("customer_id"));
        Customer customer = customerService.getOne(new QueryWrapper<Customer>().eq("id", customerId).isNull("ammeter_id"));
        if (ammeter != null && customer != null){
            //将电表表中添加挂接的客户ID
            ammeterService.update(new UpdateWrapper<AssetsAmmeter>().set("customer_id",customerId).eq("id",ammeterId));
            //将客户表中添加电表ID
            customerService.update(new UpdateWrapper<Customer>().set("ammeter_id",ammeter.getAmmeterId()).eq("id",customerId));
            return AjaxResult.success();
        }else {
            throw new LogicException("该用户已经被挂接电表");
        }
    }

    //挂接时查询电表
    @GetMapping("/mountAmmeterList")
    public AjaxResult mountAmmeterList(Long AmmeterId){
        List<AssetsAmmeter> list = ammeterService.mountAmmeterList(AmmeterId);
        return AjaxResult.success(list);
    }

    /*首页 统计信息接口*/
    @GetMapping("/count")
    public AjaxResult count(){
        int count = customerService.count(new QueryWrapper<Customer>().eq("del_flg",0));
        return AjaxResult.success(count);
    }
}

